<title>Increment and decrement.</title>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Increment and decrement.</h1>
</center>
<hr>

The traditional method of incrementing numbers is by coding something like:
<pre>
	a = a + 1;
</pre>
Within C, this syntax is valid but you can also use the ++ operator to perform
the same function.
<pre>
	a++;
</pre>
will also add 1 to the value of <b>a</b>. 
By using a simular syntax you can also decrement a variable as shown below.
<pre>
	a--;
</pre>
These operators can be placed as a prefix or post fix as below:
<pre>
	a++;		++a;
</pre>
When used on their own (as above) the prefix and postfix have the same effect
BUT within an expression there is a subtle difference....<p>

<ol>
<li>Prefix notation will increment the variable BEFORE the 
expression is evaluated.
<li>Postfix notation will increment AFTER the expression evaluation.
</ol>

Here is an example:
<pre>
	main()				main()
	{				{
	  int a=1;			  int a=1;
	  printf(" a is %d", ++a);	  printf(" a is %d", a++);
        }				}

</pre>

In both examples, the final value of <b>a</b> will be 2. BUT the first 
example will print 2 and the second will print 1.

<hr>
<img src=../../GRAPHICS/computer.gif>
<a href="../EXAMPLES/inc_dec.c"> Example program.</a><br>

<img src=../../GRAPHICS/whiteball.gif>
<a href="../CONCEPT/expressions.html">Other operators.</a><br>

<img src=../../GRAPHICS/whiteball.gif>
<a href="../CONCEPT/precedence.html">Operator precedence table.</a>
<hr>
<p>
<center>
<table border=2 width=80% bgcolor=ivory>
<tr align=center>
<td width=25%>
<a href="../cref.html">Top</a>
</td><td width=25%>
<a href="../master_index.html">Master Index</a>
</td><td width=25%>
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
 
<hr>
<address><a href="../../address.html">Martin Leslie</a>
<script language="JavaScript">
<!--  //
document.write(document.lastModified);
// -->
</script>
</address>
